﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace RemoteUpgradeDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> lastVersionFileGroup = new List<string>();
            List<string> curVersionFileGroup = new List<string>();
            string lastVersionDir = @"E:\CODE\DONET\Company\20210525PQSS\bin\Release\PQSS V1.4 - 副本";
            string curVersionDir = @"E:\CODE\DONET\Company\20210525PQSS\bin\Release\netcoreapp3.1 - 副本";
            SearchFolderDstFile(lastVersionDir, ref lastVersionFileGroup);
            SearchFolderDstFile(curVersionDir, ref curVersionFileGroup);
            foreach (string filePath in curVersionFileGroup)
            {
                // 如果不是dll文件，直接覆盖
                if (!Path.GetExtension(filePath).Equals(".dll"))
                    File.Copy(filePath, Path.Combine(lastVersionDir, Path.GetFileName(filePath)), true);
                // 如果是Ygs-PQSS.dll,直接覆盖，否则更新不会成功  此处应该放置 项目名.dll，也可能是解决方案名.dll
                else if (Path.GetFileName(filePath).Equals("Ygs-PQSS.dll"))
                    File.Copy(filePath, Path.Combine(lastVersionDir, Path.GetFileName(filePath)), true);
                // 其他文件，仅复制新增文件
                else if (!File.Exists(Path.Combine(lastVersionDir, Path.GetFileName(filePath))))
                    File.Copy(filePath, Path.Combine(lastVersionDir, Path.GetFileName(filePath)), true);
            }
            Console.ReadLine();
        }

        /// <summary>
        /// 递归检索指定文件夹下的指定文件路径
        /// </summary>
        /// <param name="sourceDir"></param>
        static void SearchFolderDstFile(string sourceDir, ref List<string> resultList)
        {
            try
            {
                DirectoryInfo thisDir = new DirectoryInfo(sourceDir);
                //判断所指的文件夹/文件是否存在  
                if (!thisDir.Exists)
                    return;

                DirectoryInfo dirInfo = thisDir as DirectoryInfo;
                //获取文件夹下所有文件和文件夹  
                FileSystemInfo[] files = dirInfo.GetFileSystemInfos();
                //对单个FileSystemInfo进行判断，如果是文件夹则进行递归操作  
                foreach (FileSystemInfo file in files)
                {
                    FileInfo fileInfo = file as FileInfo;
                    if (fileInfo != null)
                        //如果是文件，进行文件操作 
                        resultList.Add(Path.Combine(fileInfo.DirectoryName, fileInfo.Name));
                    else
                        //如果是文件夹，则进行递归调用 
                        SearchFolderDstFile(Path.Combine(sourceDir, file.ToString()), ref resultList);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("获取文件夹下所有文件出现问题：" + ex.ToString());
            }
        }
    }
}
